package com.example.singing;

import com.example.singing.manager.UserManager;
import com.example.singing.model.User;
import com.example.singing.util.ThemeManager;
import com.example.singing.view.LoginView;
import com.example.singing.view.MainView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * 歌唱比赛评分系统 - 主应用程序
 */
public class MainApp extends Application {

    private Stage primaryStage;
    private UserManager userManager;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.userManager = new UserManager();

        // 只在没有用户数据时添加默认用户
        if (userManager.getAllUsers().isEmpty()) {
            userManager.addUser(new User("admin", "管理员", "admin123", "管理员"));
            userManager.addUser(new User("judge", "评委", "judge123", "评委"));
            System.out.println("已添加默认用户");
        }

        // 显示登录界面
        showLoginView();

        primaryStage.setTitle("歌唱比赛评分系统");
        primaryStage.setResizable(true);
        primaryStage.show();
    }

    /**
     * 显示登录界面
     */
    public void showLoginView() {
        LoginView loginView = new LoginView(this, userManager);
        Scene scene = new Scene(loginView.getView(), 800, 600);
        
        // 应用主题
        ThemeManager themeManager = ThemeManager.getInstance();
        themeManager.applyTheme(scene);
        
        primaryStage.setScene(scene);
    }

    /**
     * 显示主界面
     */
    public void showMainView(User user) {
        MainView mainView = new MainView(this, user);
        Scene scene = new Scene(mainView.getView(), 1200, 700);
        
        // 应用主题
        ThemeManager themeManager = ThemeManager.getInstance();
        themeManager.applyTheme(scene);
        
        primaryStage.setScene(scene);
    }

    /**
     * 获取主舞台
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void main(String[] args) {
        launch(args);
    }
}